package com.cssnj.portal.utils;

import io.predictech.framework.domain.common.TreeNode;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TreeUtil {

    public static List<Map<String, Object>> toChlidrenListTree(List<Map<String, Object>> tList, String state) {

        List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : tList) {
            String sjgn_id = map.get(TreeNode.PID.getKey()).toString();
            if ("0".equals(map.get(TreeNode.PID.getKey())) || StringUtils.isEmpty(sjgn_id)) {
                treeList.add(map);
            } else {
                for (int i = 0; i < tList.size(); i++) {
                    Map<String, Object> obj = tList.get(i);
                    if (sjgn_id.equals(obj.get(TreeNode.ID.getKey()).toString())) {
                        if (obj.get(TreeNode.CHILDREN.getKey()) == null) {
                            obj.put(TreeNode.CHILDREN.getKey(), new ArrayList<Map<String, Object>>());
                            obj.put(TreeNode.STATE.getKey(), state);
                        }
                        ((ArrayList<Map<String, Object>>) obj.get(TreeNode.CHILDREN.getKey())).add(map);
                        break;
                    }
                }
            }
        }
        return treeList;
    }

}
